package com.pinc.akb48;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

public class main extends TabActivity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		addTab(getResources().getString(R.string.str_news), R.drawable.news,
				NewsActivity.class);
		addTab(getResources().getString(R.string.str_video), R.drawable.video,
				VideoActivity.class);
		addTab(getResources().getString(R.string.str_photo), R.drawable.photo,
				PhotoActivity.class);
	}

	private void addTab(String labelId, int drawableId, Class<?> c) {
		TabHost tabHost = getTabHost();
		Intent intent = new Intent(this, c);
		TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);

		View tabIndicator = LayoutInflater.from(this).inflate(
				R.layout.tab_indicator, getTabWidget(), false);
		TextView title = (TextView) tabIndicator.findViewById(R.id.title);
		title.setText(labelId);
		ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
		icon.setImageResource(drawableId);

		spec.setIndicator(tabIndicator);
		spec.setContent(intent);
		tabHost.addTab(spec);
	}

	@Override
	protected void onPostResume() {
		super.onPostResume();
		new Thread(new Runnable() {
			@Override
			public void run() {
				Parser_Version pv = new Parser_Version();
				String appName = getPackageName().substring(getPackageName().lastIndexOf('.')+1);
				if (pv.parse(appName)) {
					try {
						if (getPackageManager().getPackageInfo(
								getPackageName(), 0).versionCode < pv
								.getVersion())
							main.this.runOnUiThread(new Runnable() {
								@Override
								public void run() {
									showDialog(ID_CONFIRM);
								}
							});
					} catch (NameNotFoundException e) {
						e.printStackTrace();
					}
				}
			}
		}).start();
	}

	private static final int ID_CONFIRM = 1;

	@Override
	protected Dialog onCreateDialog(int id) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);

		switch (id) {
		case ID_CONFIRM: {
			builder.setMessage(
					getResources().getString(R.string.str_update_notify))
					.setPositiveButton(
							getResources().getString(R.string.str_update),
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									Uri uri = Uri
									.parse(String.format("market://details?id=%s", getPackageName()));
									Intent intent_market = new Intent(
											Intent.ACTION_VIEW, uri);
									startActivity(intent_market);
								}
							})
							.setNegativeButton(
									getResources().getString(R.string.str_later), null);
			break;
		}
		default:
			break;
		}
		return builder.create();
	}

}